package com.zenithmind.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.chat.pojo.entity.ChatGroupMemberSetting;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 群组成员设置映射器 - 遵循单一职责原则
 * 只负责群组成员设置的数据访问操作
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface ChatGroupMemberSettingMapper extends BaseMapper<ChatGroupMemberSetting> {

    /**
     * 根据群组ID和用户ID查询成员设置
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @return 成员设置
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    ChatGroupMemberSetting selectByGroupIdAndUserId(@Param("groupId") String groupId, @Param("userId") String userId);

    /**
     * 根据群组ID查询所有成员设置
     * 
     * @param groupId 群组ID
     * @return 成员设置列表
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE group_id = #{groupId} AND deleted = 0")
    List<ChatGroupMemberSetting> selectByGroupId(@Param("groupId") String groupId);

    /**
     * 根据用户ID查询所有群组成员设置
     * 
     * @param userId 用户ID
     * @return 成员设置列表
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE user_id = #{userId} AND deleted = 0")
    List<ChatGroupMemberSetting> selectByUserId(@Param("userId") String userId);

    /**
     * 查询群组中置顶的成员设置
     * 
     * @param groupId 群组ID
     * @return 置顶成员设置列表
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE group_id = #{groupId} AND is_top = 1 AND deleted = 0")
    List<ChatGroupMemberSetting> selectTopMembersByGroupId(@Param("groupId") String groupId);

    /**
     * 查询群组中免打扰的成员设置
     * 
     * @param groupId 群组ID
     * @return 免打扰成员设置列表
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE group_id = #{groupId} AND is_muted = 1 AND deleted = 0")
    List<ChatGroupMemberSetting> selectMutedMembersByGroupId(@Param("groupId") String groupId);

    /**
     * 查询用户置顶的群组设置
     * 
     * @param userId 用户ID
     * @return 置顶群组设置列表
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE user_id = #{userId} AND is_top = 1 AND deleted = 0")
    List<ChatGroupMemberSetting> selectTopGroupsByUserId(@Param("userId") String userId);

    /**
     * 查询用户免打扰的群组设置
     * 
     * @param userId 用户ID
     * @return 免打扰群组设置列表
     */
    @Select("SELECT * FROM chat_group_member_setting WHERE user_id = #{userId} AND is_muted = 1 AND deleted = 0")
    List<ChatGroupMemberSetting> selectMutedGroupsByUserId(@Param("userId") String userId);

    /**
     * 更新成员昵称
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @param nickname 昵称
     * @return 更新结果
     */
    @Update("UPDATE chat_group_member_setting SET nickname = #{nickname}, update_time = NOW() " +
            "WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int updateNickname(@Param("groupId") String groupId, @Param("userId") String userId, @Param("nickname") String nickname);

    /**
     * 更新置顶状态
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @param isTop 是否置顶
     * @return 更新结果
     */
    @Update("UPDATE chat_group_member_setting SET is_top = #{isTop}, update_time = NOW() " +
            "WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int updateTopStatus(@Param("groupId") String groupId, @Param("userId") String userId, @Param("isTop") Boolean isTop);

    /**
     * 更新免打扰状态
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @param isMuted 是否免打扰
     * @return 更新结果
     */
    @Update("UPDATE chat_group_member_setting SET is_muted = #{isMuted}, update_time = NOW() " +
            "WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int updateMuteStatus(@Param("groupId") String groupId, @Param("userId") String userId, @Param("isMuted") Boolean isMuted);

    /**
     * 更新消息提醒设置
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @param messageNotification 消息提醒设置
     * @return 更新结果
     */
    @Update("UPDATE chat_group_member_setting SET message_notification = #{messageNotification}, update_time = NOW() " +
            "WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int updateMessageNotification(@Param("groupId") String groupId, @Param("userId") String userId, 
                                 @Param("messageNotification") Integer messageNotification);

    /**
     * 更新显示成员昵称设置
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @param showMemberNickname 是否显示成员昵称
     * @return 更新结果
     */
    @Update("UPDATE chat_group_member_setting SET show_member_nickname = #{showMemberNickname}, update_time = NOW() " +
            "WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int updateShowMemberNickname(@Param("groupId") String groupId, @Param("userId") String userId, 
                                @Param("showMemberNickname") Boolean showMemberNickname);

    /**
     * 更新保存到通讯录设置
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @param saveToContacts 是否保存到通讯录
     * @return 更新结果
     */
    @Update("UPDATE chat_group_member_setting SET save_to_contacts = #{saveToContacts}, update_time = NOW() " +
            "WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int updateSaveToContacts(@Param("groupId") String groupId, @Param("userId") String userId, 
                            @Param("saveToContacts") Boolean saveToContacts);

    /**
     * 检查成员设置是否存在
     * 
     * @param groupId 群组ID
     * @param userId 用户ID
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) FROM chat_group_member_setting WHERE group_id = #{groupId} AND user_id = #{userId} AND deleted = 0")
    int checkSettingExists(@Param("groupId") String groupId, @Param("userId") String userId);

    /**
     * 统计群组中置顶成员数量
     * 
     * @param groupId 群组ID
     * @return 置顶成员数量
     */
    @Select("SELECT COUNT(*) FROM chat_group_member_setting WHERE group_id = #{groupId} AND is_top = 1 AND deleted = 0")
    int countTopMembersByGroupId(@Param("groupId") String groupId);

    /**
     * 统计群组中免打扰成员数量
     * 
     * @param groupId 群组ID
     * @return 免打扰成员数量
     */
    @Select("SELECT COUNT(*) FROM chat_group_member_setting WHERE group_id = #{groupId} AND is_muted = 1 AND deleted = 0")
    int countMutedMembersByGroupId(@Param("groupId") String groupId);

    /**
     * 统计用户置顶群组数量
     * 
     * @param userId 用户ID
     * @return 置顶群组数量
     */
    @Select("SELECT COUNT(*) FROM chat_group_member_setting WHERE user_id = #{userId} AND is_top = 1 AND deleted = 0")
    int countTopGroupsByUserId(@Param("userId") String userId);

    /**
     * 统计用户免打扰群组数量
     * 
     * @param userId 用户ID
     * @return 免打扰群组数量
     */
    @Select("SELECT COUNT(*) FROM chat_group_member_setting WHERE user_id = #{userId} AND is_muted = 1 AND deleted = 0")
    int countMutedGroupsByUserId(@Param("userId") String userId);

    /**
     * 批量删除群组的所有成员设置
     * 
     * @param groupId 群组ID
     * @return 删除数量
     */
    @Update("UPDATE chat_group_member_setting SET deleted = 1, update_time = NOW() WHERE group_id = #{groupId} AND deleted = 0")
    int batchDeleteByGroupId(@Param("groupId") String groupId);

    /**
     * 批量删除用户的所有群组设置
     * 
     * @param userId 用户ID
     * @return 删除数量
     */
    @Update("UPDATE chat_group_member_setting SET deleted = 1, update_time = NOW() WHERE user_id = #{userId} AND deleted = 0")
    int batchDeleteByUserId(@Param("userId") String userId);
}
